import {query} from "../services/users";

export default {
  namespace: 'users',
  state: {
    list: [],
    curPage: null,
  },
  reducers: {
    querySuccess(state, action) {
      return {...state, ...action.payload};
    }
  },
  effects: {
    *query({payload: {page, referer}}, {call, put}) {
      const response = yield call(query, {page, referer})
      if (response) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: response.data.json,
          }
        })
      }
    }
  },
  subscriptions: {
    setup({dispatch, history}) {
      history.listen(location => {
        if (location.pathname === '/users') {
          dispatch({
            type: 'query',
            payload: location.query,
          });
        }
      });
    }
  },
};

